if (!("finalizeConstruction" in ViewPU.prototype)) {
    Reflect.set(ViewPU.prototype, "finalizeConstruction", () => { });
}
interface Dialog_Params {
    controller?: CustomDialogController;
}
interface SliderCustom_Params {
    currentIndex?: number;
    currentAdjustData?: number[];
    min?: number;
    max?: number;
    pixelMap?: image.PixelMap;
    isPixelMapChange?: boolean;
    postState?: boolean;
    saturationLastSlider?: number;
    brightnessLastSlider?: number;
    deviceListDialogController?: CustomDialogController;
}
interface AdjustContentView_Params {
    currentAdjustIndex?: number;
    currentAdjustData?: Array<number>;
    AdjustIconList?: Array<IconStatus>;
}
import type image from "@ohos:multimedia.image";
import worker from "@ohos:worker";
import type { MessageEvents } from "@ohos:worker";
import { adjustIconList } from "@bundle:com.example.imagetestharmony/entry/ets/viewModel/IconListViewModel";
import type { IconStatus } from "@bundle:com.example.imagetestharmony/entry/ets/viewModel/IconListViewModel";
import { adjustOpacity } from "@bundle:com.example.imagetestharmony/entry/ets/utils/OpacityUtil";
import { CommonConstants } from "@bundle:com.example.imagetestharmony/entry/ets/common/constant/CommonConstants";
import { AdjustId } from "@bundle:com.example.imagetestharmony/entry/ets/viewModel/OptionViewModel";
import { MessageItem } from "@bundle:com.example.imagetestharmony/entry/ets/viewModel/MessageItem";
import Logger from "@bundle:com.example.imagetestharmony/entry/ets/utils/LoggerUtil";
export default class AdjustContentView extends ViewPU {
    constructor(parent, params, __localStorage, elmtId = -1, paramsLambda = undefined, extraInfo) {
        super(parent, __localStorage, elmtId, extraInfo);
        if (typeof paramsLambda === "function") {
            this.paramsGenerator_ = paramsLambda;
        }
        this.__currentAdjustIndex = new ObservedPropertySimplePU(0, this, "currentAdjustIndex");
        this.__currentAdjustData = this.initializeConsume('currentAdjustData', "currentAdjustData");
        this.AdjustIconList = adjustIconList;
        this.setInitiallyProvidedValue(params);
        this.finalizeConstruction();
    }
    setInitiallyProvidedValue(params: AdjustContentView_Params) {
        if (params.currentAdjustIndex !== undefined) {
            this.currentAdjustIndex = params.currentAdjustIndex;
        }
        if (params.AdjustIconList !== undefined) {
            this.AdjustIconList = params.AdjustIconList;
        }
    }
    updateStateVars(params: AdjustContentView_Params) {
    }
    purgeVariableDependenciesOnElmtId(rmElmtId) {
        this.__currentAdjustIndex.purgeDependencyOnElmtId(rmElmtId);
        this.__currentAdjustData.purgeDependencyOnElmtId(rmElmtId);
    }
    aboutToBeDeleted() {
        this.__currentAdjustIndex.aboutToBeDeleted();
        this.__currentAdjustData.aboutToBeDeleted();
        SubscriberManager.Get().delete(this.id__());
        this.aboutToBeDeletedInternal();
    }
    TabBuilder(index: number, name: string | Resource, parent = null) {
        this.observeComponentCreation2((elmtId, isInitialRender) => {
            Column.create();
            Column.width(CommonConstants.LAYOUT_FULL_SCREEN);
        }, Column);
        this.observeComponentCreation2((elmtId, isInitialRender) => {
            Image.create(this.currentAdjustIndex === index ? this.AdjustIconList[index]?.chosen : this.AdjustIconList[index]?.normal);
            Image.width({ "id": 16777237, "type": 10002, params: [], "bundleName": "com.example.imagetestharmony", "moduleName": "entry" });
            Image.height({ "id": 16777236, "type": 10002, params: [], "bundleName": "com.example.imagetestharmony", "moduleName": "entry" });
        }, Image);
        this.observeComponentCreation2((elmtId, isInitialRender) => {
            Text.create(name);
            Text.fontColor(this.currentAdjustIndex === index ? Color.Blue : Color.White);
            Text.fontSize({ "id": 16777235, "type": 10002, params: [], "bundleName": "com.example.imagetestharmony", "moduleName": "entry" });
            Text.margin({ top: { "id": 16777239, "type": 10002, params: [], "bundleName": "com.example.imagetestharmony", "moduleName": "entry" } });
        }, Text);
        Text.pop();
        Column.pop();
    }
    private __currentAdjustIndex: ObservedPropertySimplePU<number>;
    get currentAdjustIndex() {
        return this.__currentAdjustIndex.get();
    }
    set currentAdjustIndex(newValue: number) {
        this.__currentAdjustIndex.set(newValue);
    }
    private __currentAdjustData: ObservedPropertyAbstractPU<Array<number>>;
    get currentAdjustData() {
        return this.__currentAdjustData.get();
    }
    set currentAdjustData(newValue: Array<number>) {
        this.__currentAdjustData.set(newValue);
    }
    private AdjustIconList: Array<IconStatus>;
    initialRender() {
        this.observeComponentCreation2((elmtId, isInitialRender) => {
            Tabs.create({ barPosition: BarPosition.End });
            Tabs.margin({ bottom: { "id": 16777238, "type": 10002, params: [], "bundleName": "com.example.imagetestharmony", "moduleName": "entry" } });
            Tabs.onChange((index: number) => {
                this.currentAdjustIndex = index;
            });
        }, Tabs);
        this.observeComponentCreation2((elmtId, isInitialRender) => {
            TabContent.create(() => {
                {
                    this.observeComponentCreation2((elmtId, isInitialRender) => {
                        if (isInitialRender) {
                            let componentCall = new SliderCustom(this, {
                                currentIndex: AdjustId.BRIGHTNESS.valueOf(),
                                min: CommonConstants.SLIDER_MIN.valueOf(),
                                max: CommonConstants.SLIDER_MAX.valueOf(),
                                currentAdjustData: this.__currentAdjustData
                            }, undefined, elmtId, () => { }, { page: "entry/src/main/ets/view/AdjustContentView.ets", line: 35, col: 9 });
                            ViewPU.create(componentCall);
                            let paramsLambda = () => {
                                return {
                                    currentIndex: AdjustId.BRIGHTNESS.valueOf(),
                                    min: CommonConstants.SLIDER_MIN.valueOf(),
                                    max: CommonConstants.SLIDER_MAX.valueOf(),
                                    currentAdjustData: this.currentAdjustData
                                };
                            };
                            componentCall.paramsGenerator_ = paramsLambda;
                        }
                        else {
                            this.updateStateVarsOfChildByElmtId(elmtId, {
                                currentIndex: AdjustId.BRIGHTNESS.valueOf(),
                                min: CommonConstants.SLIDER_MIN.valueOf(),
                                max: CommonConstants.SLIDER_MAX.valueOf()
                            });
                        }
                    }, { name: "SliderCustom" });
                }
            });
            TabContent.tabBar({ builder: () => {
                    this.TabBuilder.call(this, AdjustId.BRIGHTNESS, { "id": 16777221, "type": 10003, params: [], "bundleName": "com.example.imagetestharmony", "moduleName": "entry" });
                } });
        }, TabContent);
        TabContent.pop();
        this.observeComponentCreation2((elmtId, isInitialRender) => {
            TabContent.create(() => {
                {
                    this.observeComponentCreation2((elmtId, isInitialRender) => {
                        if (isInitialRender) {
                            let componentCall = new SliderCustom(this, {
                                currentIndex: AdjustId.TRANSPARENCY.valueOf(),
                                min: CommonConstants.SLIDER_MIN.valueOf(),
                                max: CommonConstants.SLIDER_MAX.valueOf(),
                                currentAdjustData: this.__currentAdjustData
                            }, undefined, elmtId, () => { }, { page: "entry/src/main/ets/view/AdjustContentView.ets", line: 45, col: 9 });
                            ViewPU.create(componentCall);
                            let paramsLambda = () => {
                                return {
                                    currentIndex: AdjustId.TRANSPARENCY.valueOf(),
                                    min: CommonConstants.SLIDER_MIN.valueOf(),
                                    max: CommonConstants.SLIDER_MAX.valueOf(),
                                    currentAdjustData: this.currentAdjustData
                                };
                            };
                            componentCall.paramsGenerator_ = paramsLambda;
                        }
                        else {
                            this.updateStateVarsOfChildByElmtId(elmtId, {
                                currentIndex: AdjustId.TRANSPARENCY.valueOf(),
                                min: CommonConstants.SLIDER_MIN.valueOf(),
                                max: CommonConstants.SLIDER_MAX.valueOf()
                            });
                        }
                    }, { name: "SliderCustom" });
                }
            });
            TabContent.tabBar({ builder: () => {
                    this.TabBuilder.call(this, AdjustId.TRANSPARENCY, { "id": 16777233, "type": 10003, params: [], "bundleName": "com.example.imagetestharmony", "moduleName": "entry" });
                } });
        }, TabContent);
        TabContent.pop();
        this.observeComponentCreation2((elmtId, isInitialRender) => {
            TabContent.create(() => {
                {
                    this.observeComponentCreation2((elmtId, isInitialRender) => {
                        if (isInitialRender) {
                            let componentCall = new SliderCustom(this, {
                                currentIndex: AdjustId.SATURATION.valueOf(),
                                min: CommonConstants.SLIDER_MIN.valueOf(),
                                max: CommonConstants.SLIDER_MAX.valueOf(),
                                currentAdjustData: this.__currentAdjustData
                            }, undefined, elmtId, () => { }, { page: "entry/src/main/ets/view/AdjustContentView.ets", line: 55, col: 9 });
                            ViewPU.create(componentCall);
                            let paramsLambda = () => {
                                return {
                                    currentIndex: AdjustId.SATURATION.valueOf(),
                                    min: CommonConstants.SLIDER_MIN.valueOf(),
                                    max: CommonConstants.SLIDER_MAX.valueOf(),
                                    currentAdjustData: this.currentAdjustData
                                };
                            };
                            componentCall.paramsGenerator_ = paramsLambda;
                        }
                        else {
                            this.updateStateVarsOfChildByElmtId(elmtId, {
                                currentIndex: AdjustId.SATURATION.valueOf(),
                                min: CommonConstants.SLIDER_MIN.valueOf(),
                                max: CommonConstants.SLIDER_MAX.valueOf()
                            });
                        }
                    }, { name: "SliderCustom" });
                }
            });
            TabContent.tabBar({ builder: () => {
                    this.TabBuilder.call(this, AdjustId.SATURATION, { "id": 16777230, "type": 10003, params: [], "bundleName": "com.example.imagetestharmony", "moduleName": "entry" });
                } });
        }, TabContent);
        TabContent.pop();
        Tabs.pop();
    }
    rerender() {
        this.updateDirtyElements();
    }
}
class SliderCustom extends ViewPU {
    constructor(parent, params, __localStorage, elmtId = -1, paramsLambda = undefined, extraInfo) {
        super(parent, __localStorage, elmtId, extraInfo);
        if (typeof paramsLambda === "function") {
            this.paramsGenerator_ = paramsLambda;
        }
        this.__currentIndex = new SynchedPropertySimpleOneWayPU(params.currentIndex, this, "currentIndex");
        this.__currentAdjustData = new SynchedPropertyObjectTwoWayPU(params.currentAdjustData, this, "currentAdjustData");
        this.__min = new SynchedPropertySimpleOneWayPU(params.min, this, "min");
        this.__max = new SynchedPropertySimpleOneWayPU(params.max, this, "max");
        this.__pixelMap = this.initializeConsume('pixelMap', "pixelMap");
        this.__isPixelMapChange = this.initializeConsume('isPixelMapChange', "isPixelMapChange");
        this.postState = true;
        this.saturationLastSlider = CommonConstants.SLIDER_MAX;
        this.brightnessLastSlider = CommonConstants.SLIDER_MAX;
        this.deviceListDialogController = new CustomDialogController({
            builder: () => {
                let jsDialog = new Dialog(this, {}, undefined, -1, () => { }, { page: "entry/src/main/ets/view/AdjustContentView.ets", line: 83, col: 14 });
                jsDialog.setController(this.deviceListDialogController);
                ViewPU.create(jsDialog);
                let paramsLambda = () => {
                    return {};
                };
                jsDialog.paramsGenerator_ = paramsLambda;
            },
            alignment: DialogAlignment.Center,
            autoCancel: false,
            customStyle: true
        }, this);
        this.setInitiallyProvidedValue(params);
        this.finalizeConstruction();
    }
    setInitiallyProvidedValue(params: SliderCustom_Params) {
        if (params.postState !== undefined) {
            this.postState = params.postState;
        }
        if (params.saturationLastSlider !== undefined) {
            this.saturationLastSlider = params.saturationLastSlider;
        }
        if (params.brightnessLastSlider !== undefined) {
            this.brightnessLastSlider = params.brightnessLastSlider;
        }
        if (params.deviceListDialogController !== undefined) {
            this.deviceListDialogController = params.deviceListDialogController;
        }
    }
    updateStateVars(params: SliderCustom_Params) {
        this.__currentIndex.reset(params.currentIndex);
        this.__min.reset(params.min);
        this.__max.reset(params.max);
    }
    purgeVariableDependenciesOnElmtId(rmElmtId) {
        this.__currentIndex.purgeDependencyOnElmtId(rmElmtId);
        this.__currentAdjustData.purgeDependencyOnElmtId(rmElmtId);
        this.__min.purgeDependencyOnElmtId(rmElmtId);
        this.__max.purgeDependencyOnElmtId(rmElmtId);
        this.__pixelMap.purgeDependencyOnElmtId(rmElmtId);
        this.__isPixelMapChange.purgeDependencyOnElmtId(rmElmtId);
    }
    aboutToBeDeleted() {
        this.__currentIndex.aboutToBeDeleted();
        this.__currentAdjustData.aboutToBeDeleted();
        this.__min.aboutToBeDeleted();
        this.__max.aboutToBeDeleted();
        this.__pixelMap.aboutToBeDeleted();
        this.__isPixelMapChange.aboutToBeDeleted();
        SubscriberManager.Get().delete(this.id__());
        this.aboutToBeDeletedInternal();
    }
    private __currentIndex: SynchedPropertySimpleOneWayPU<number>;
    get currentIndex() {
        return this.__currentIndex.get();
    }
    set currentIndex(newValue: number) {
        this.__currentIndex.set(newValue);
    }
    private __currentAdjustData: SynchedPropertySimpleOneWayPU<number[]>;
    get currentAdjustData() {
        return this.__currentAdjustData.get();
    }
    set currentAdjustData(newValue: number[]) {
        this.__currentAdjustData.set(newValue);
    }
    private __min: SynchedPropertySimpleOneWayPU<number>;
    get min() {
        return this.__min.get();
    }
    set min(newValue: number) {
        this.__min.set(newValue);
    }
    private __max: SynchedPropertySimpleOneWayPU<number>;
    get max() {
        return this.__max.get();
    }
    set max(newValue: number) {
        this.__max.set(newValue);
    }
    private __pixelMap?: ObservedPropertyAbstractPU<image.PixelMap>;
    get pixelMap() {
        return this.__pixelMap.get();
    }
    set pixelMap(newValue: image.PixelMap) {
        this.__pixelMap.set(newValue);
    }
    private __isPixelMapChange: ObservedPropertyAbstractPU<boolean>;
    get isPixelMapChange() {
        return this.__isPixelMapChange.get();
    }
    set isPixelMapChange(newValue: boolean) {
        this.__isPixelMapChange.set(newValue);
    }
    private postState: boolean;
    private saturationLastSlider: number;
    private brightnessLastSlider: number;
    private deviceListDialogController: CustomDialogController;
    initialRender() {
        this.observeComponentCreation2((elmtId, isInitialRender) => {
            Column.create();
        }, Column);
        this.observeComponentCreation2((elmtId, isInitialRender) => {
            Text.create(`${this.currentAdjustData[this.currentIndex]}`);
            Text.fontColor(Color.White);
            Text.margin({ top: { "id": 16777247, "type": 10002, params: [], "bundleName": "com.example.imagetestharmony", "moduleName": "entry" } });
            Text.fontSize({ "id": 16777246, "type": 10002, params: [], "bundleName": "com.example.imagetestharmony", "moduleName": "entry" });
        }, Text);
        Text.pop();
        this.observeComponentCreation2((elmtId, isInitialRender) => {
            Row.create();
            Row.width(CommonConstants.LAYOUT_FULL_SCREEN);
            Row.justifyContent(FlexAlign.Center);
        }, Row);
        this.observeComponentCreation2((elmtId, isInitialRender) => {
            Slider.create({
                value: this.currentAdjustData[this.currentIndex],
                step: CommonConstants.SLIDER_STEP,
                min: this.min,
                max: this.max
            });
            Slider.trackColor(Color.White);
            Slider.width(CommonConstants.SLIDER_WIDTH);
            Slider.showSteps(true);
            Slider.onChange((value: number, mode: SliderChangeMode) => {
                this.sliderChange(value > this.max ? this.max : value, mode);
            });
        }, Slider);
        Row.pop();
        Column.pop();
    }
    sliderChange(value: number, mode: SliderChangeMode) {
        if ((mode === SliderChangeMode.End) && (value !== this.currentAdjustData[this.currentIndex])) {
            this.currentAdjustData[this.currentIndex] = Math.round(value);
            switch (this.currentIndex) {
                case AdjustId.BRIGHTNESS:
                    this.postToWorker(AdjustId.BRIGHTNESS, value, CommonConstants.BRIGHTNESS_WORKER_FILE);
                    break;
                case AdjustId.TRANSPARENCY:
                    if (this.pixelMap) {
                        adjustOpacity(this.pixelMap, Math.round(value))
                            .then((pixelMap?: image.PixelMap) => {
                            if (pixelMap) {
                                this.pixelMap = pixelMap;
                                this.isPixelMapChange = !this.isPixelMapChange;
                            }
                        });
                    }
                    break;
                case AdjustId.SATURATION:
                    this.postToWorker(AdjustId.SATURATION, value, CommonConstants.SATURATION_WORKER_FILE);
                    break;
                default:
                    break;
            }
        }
    }
    postToWorker(type: AdjustId, value: number, workerName: string) {
        if (!this.pixelMap) {
            return;
        }
        let sliderValue = type === AdjustId.BRIGHTNESS ? this.brightnessLastSlider : this.saturationLastSlider;
        let workerInstance = new worker.ThreadWorker(workerName);
        const bufferArray = new ArrayBuffer(this.pixelMap.getPixelBytesNumber());
        this.pixelMap.readPixelsToBuffer(bufferArray)
            .then(() => {
            let message = new MessageItem(bufferArray, sliderValue, value);
            workerInstance.postMessage(message);
            if (this.postState) {
                this.deviceListDialogController.open();
            }
            this.postState = false;
            workerInstance.onmessage = (event: MessageEvents) => {
                this.updatePixelMap(event);
                workerInstance.terminate();
            };
            if (type === AdjustId.BRIGHTNESS) {
                this.brightnessLastSlider = Math.round(value);
            }
            else {
                this.saturationLastSlider = Math.round(value);
            }
            workerInstance.onexit = () => {
                if (workerInstance !== undefined) {
                    workerInstance.terminate();
                    Logger.info('AdjustContentView', 'main thread terminate');
                }
            };
        });
    }
    updatePixelMap(event: MessageEvents) {
        const newPixel = this.pixelMap as image.PixelMap;
        newPixel.writeBufferToPixels(event.data);
        this.pixelMap = newPixel;
        this.isPixelMapChange = !this.isPixelMapChange;
        this.deviceListDialogController.close();
        this.postState = true;
    }
    rerender() {
        this.updateDirtyElements();
    }
}
export class Dialog extends ViewPU {
    constructor(parent, params, __localStorage, elmtId = -1, paramsLambda = undefined, extraInfo) {
        super(parent, __localStorage, elmtId, extraInfo);
        if (typeof paramsLambda === "function") {
            this.paramsGenerator_ = paramsLambda;
        }
        this.controller = undefined;
        this.setInitiallyProvidedValue(params);
        this.finalizeConstruction();
    }
    setInitiallyProvidedValue(params: Dialog_Params) {
        if (params.controller !== undefined) {
            this.controller = params.controller;
        }
    }
    updateStateVars(params: Dialog_Params) {
    }
    purgeVariableDependenciesOnElmtId(rmElmtId) {
    }
    aboutToBeDeleted() {
        SubscriberManager.Get().delete(this.id__());
        this.aboutToBeDeletedInternal();
    }
    private controller?: CustomDialogController;
    setController(ctr: CustomDialogController) {
        this.controller = ctr;
    }
    initialRender() {
        this.observeComponentCreation2((elmtId, isInitialRender) => {
            Column.create();
        }, Column);
        this.observeComponentCreation2((elmtId, isInitialRender) => {
            LoadingProgress.create();
            LoadingProgress.color(Color.White);
            LoadingProgress.width(CommonConstants.LOADING_WH);
            LoadingProgress.height(CommonConstants.LOADING_WH);
        }, LoadingProgress);
        Column.pop();
    }
    rerender() {
        this.updateDirtyElements();
    }
}
